package com.jsj.controller;

import com.jsj.domain.Sclass;
import com.jsj.service.SclassService;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/sclass")
public class SclassController {
    private SclassService sclassService;

    @Autowired
    public void setSclassService(SclassService sclassService) {
        this.sclassService = sclassService;
    }

    /**
     * 查询
     */
    @RequestMapping(value = "/query/{id}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public Sclass query(@PathVariable("id") int id) {
        Sclass sclass = sclassService.query(id);
        return sclass;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult delete(@RequestBody int id) {
        if (sclassService.delete(id) == 0) {
            return AjaxResult.error("删除" + id + "失败");
        }
        return AjaxResult.success("删除" + id + "成功");
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult update(@RequestBody Sclass sclass) {
        if (sclassService.update(sclass) == 0) {
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success("修改成功");
    }

    /**
     * 插入
     */
    @RequestMapping(value = "/insert", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult insert(@RequestBody Sclass sclass) {
        if (sclassService.insert(sclass) == 0) {
            return AjaxResult.error("创建" + sclass.getName() + "失败");
        }
        return AjaxResult.success("创建" + sclass.getName() + "成功");
    }
}
